<include file="Common:header" />
<!-- /. NAV SIDE  -->
<include file="Common:left" />
<!-- /. NAV SIDE  -->
<style>
	.hg{
		line-height:30px;
	}
	.hg1{
		line-height:40px;
	}
	.align{
		text-align:center
	}
	</style>
<div id="page-wrapper">
	<div class="header">
		<h1 class="page-header">
			账户管理 <small>Welcome {$Think.config.system_tag}</small>
		</h1>
		<ol class="breadcrumb">
			<li><a href="{:U('Index/index')}">首页</a></li>
			<li class="active"><a href="{:U('Member/memberRoleList')}">角色列表</a></li>
			<li class="active">角色授权</li>
			<div style="float:right"><a href="javascript:history.go(-1);">返回 </a></div>
		</ol> 
	</div>
	<div id="page-inner">
		<div class="row">
			<div class="col-xs-12">
				<div class="panel panel-default">
					<div class="panel-body">
						<form class="layui-form" action="" id="postform">
							<input type="hidden" name="id" value="{$id}">
							<div class="pparent">
								<div class="hg" style="margin-bottom:30px;"><input type="checkbox" lay-skin="primary" title="全选" lay-filter="pparent" /></div>
								<volist name="nodeList" id="vo">
									<div class="parent">
									<b><input lay-filter="parent" name="node[]" type="checkbox" value="{$vo.id}|{$vo.level}|{$vo.pid}" lay-skin="primary" title="{$vo.title}" <if condition="!empty($vo['selected'])">checked</if> /></b><hr>
										<div class="son">
										<if condition="!empty($vo['snodeList'])">
											<div class="cl" style="margin-bottom:30px;">
												<volist name="vo['snodeList']" id="v">
													<div class="layui-inline hg">
														<input lay-filter="son" name="node[]" type="checkbox" value="{$v.id}|{$v.level}|{$v.pid}" lay-skin="primary" title="{$v.title}" <if condition="!empty($v['selected'])">checked</if> />
													</div>
												</volist>
											</div>
										</if>
										</div>
									</div>
								</volist>
							</div>
							<div class="layui-form-item">
								<div class="layui-input-block">
								  <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
								  <button type="reset" class="layui-btn layui-btn-primary">重置</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
	<include file="Common:foot" />
	<script>
		layui.use('form', function(){
			var form = layui.form,
				$ =layui.jquery;
			form.on('checkbox(pparent)', function(data){
				console.log(data);
				if(data.elem.checked==true){
					data.othis.parent('.hg').parent('.pparent').find('.parent input').prop('checked',true);
				}else{
					data.othis.parent('.hg').parent('.pparent').find('.parent input').prop('checked',false)
				}
				form.render();
			});
			form.on('checkbox(parent)', function(data){
				if(data.elem.checked==true){
					data.othis.parent('b').parent('.parent').find('.son input').prop('checked',true)
				}else{
					data.othis.parent('b').parent('.parent').find('.son input').prop('checked',false)
				}
				form.render();
			});
			form.on('checkbox(son)', function(data){
				if(data.elem.checked==true){
					data.othis.parent('.hg').parent('.cl').parent('.son').parent('.parent').find('input').first().prop('checked',true);
				}else{
					let make = false;
					//实现取消子类节点checked时，若不是全部子类取消  父类不能取消
					data.othis.parent('.hg').parent('.cl').parent('.son').find('input').each(function(){
					// console.log($(this).prop('checked')) 打印返回结果是  选中是true  未选中是false
						if(($(this).prop('checked')) == true){
							make=true;
						}
					})
					if(make==false){
						data.othis.parent('.hg').parent('.cl').parent('.son').parent('.parent').find('input').first().prop('checked',false);
					}
				}
				form.render();
			});
			//监听提交
			form.on('submit(formDemo)', function(data){
				var cont = $('#postform').serialize();
				$.ajax({
					url:"{:U('Member/authMemberRole')}",
					type:'post',
					dataType:'json',
					data:cont,
					success:function(data){
						if(data.code==0){
							layer.alert(data.message);
						}else{
							layer.alert(data.message, function(index){
								//do something
								//window.parent.location.reload();
								//var index = parent.layer.getFrameIndex(window.name); 
								//parent.layer.close(index);
								window.location.href="{:U('Member/memberRoleList')}";
							});
						}
					}
				});
				//layer.msg(JSON.stringify(data.field));
				return false;
			});
		});
	</script>
</div>
<!-- /. PAGE INNER  -->
<include file="Common:footer" />